﻿namespace PowerSong.UserInterface
    {
    using System.Windows;
    using System.Windows.Threading;
    using System.ComponentModel;
    using PowerSong.UserInterface.Tasks;

    /// <summary>
    /// Interaction logic for ProgressWindow.xaml
    /// </summary>
    public partial class ProgressWindow : Window
        {

        private BackgroundWorker worker = null;

        private Task task = null;

        private ProgressWindow(Task task)
            {

            InitializeComponent();
            this.Loaded += ProgressWindow_Loaded;

            // Create the worker            
            this.task = task;
            this.task.ReportProgress += task_ReportProgress;
            this.worker = new BackgroundWorker() { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
            this.worker.DoWork += worker_DoWork;
            this.worker.ProgressChanged += worker_ProgressChanged;
            this.worker.RunWorkerCompleted += worker_RunWorkerCompleted;

            // Set control values
            this.Title = task.TaskTitle;
            this.DescriptionTextBlock.Text = task.TaskDescription;
            this.StatusTextBlock.Text = string.Empty;

            }

        private void ProgressWindow_Loaded(object sender, RoutedEventArgs e)
            {
            this.worker.RunWorkerAsync();
            }

        public static void Start(Task task)
            {
            var window = new ProgressWindow(task);
            window.ShowDialog();
            }

        private void task_ReportProgress(int percentage, string status)
            {
            worker.ReportProgress(percentage);
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                           new System.Action(() => StatusTextBlock.Text = status));
            }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
            {
            task.Execute();
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                                       new System.Action(() => this.DialogResult = true ));
            }

        private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
            ProgressBar.IsIndeterminate = e.ProgressPercentage == 0;
            ProgressBar.Value = e.ProgressPercentage;
            }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
            ProgressBar.IsIndeterminate = false;
            ProgressBar.Value = 100;
            if (e.Error != null) task.HandleException(e.Error);
            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            if (worker.IsBusy && worker.WorkerSupportsCancellation)
                {
                StatusTextBlock.Text = "Cancelling...";
                CancelButton.IsEnabled = false;
                worker.CancelAsync();
                }
            }

        }

    }
